layui.config({
    base : "/js/"
}).extend({
    "mframe" : "mframe"
})

layui.use(['form','layer','mframe'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        mframe = layui.mframe,
        $ = layui.jquery;
    
    var tree = {};
    $(".close").click(function(e){
    	var index = parent.layer.getFrameIndex(window.name);
    	parent.layer.close(index);
    })
    			
    var setting = {
    	check: { enable: true, nocheckInherit: true, chkboxType: { "Y": "ps", "N": "ps" } },
        view: { selectedMulti: false, nameIsHTML: true },
        data: { key: { title: "title" }, simpleData: { enable: true } },
    };
    var url = ctx + "system/menu/roleMenuTreeData?roleId=" + $("#roleId").val();
    
    $.get(url, function(data) {
    	tree = $.fn.zTree.init($("#tree"), setting, data);
	});
    
    form.on("submit(add)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        var menuIds = mframe.Tree.getCheckedNodes(tree);
        $.ajax({
    		type : "POST",
    		url : ctx + "system/role/edit",
    		data : {
    			"roleId": data.field.roleId,
    			"roleName": data.field.roleName,
				"roleKey": data.field.roleKey,
				"roleSort": data.field.roleSort,
				"status": 0,
				"remark": data.field.remark,
				"menuIds": menuIds
    		},
    		success : function(data) {
    			top.layer.close(index);
                top.layer.msg(data.msg);
                //刷新父页面
                parent.location.reload();
    		}
    	});
        return false;
    })
})